package com.ckd.flyingtrip.adapter;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.activity.MyfaultActivity;
import com.ckd.flyingtrip.utils.Tools;

import java.util.List;


/**
 * Created by HITMAN on 2017/9/6 0006.
 */
public class GridViewAdapter extends BaseAdapter {
    private MyfaultActivity context;
    private List<String> list;
    LayoutInflater layoutInflater;
    private ImageView mImageView;
    private ImageView del;

    public GridViewAdapter(MyfaultActivity context, List<String> list) {
        this.context = context;
        this.list = list;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size() + 1;//注意此处
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("ViewHolder")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Log.i("刷新",position+"");
        convertView = layoutInflater.inflate(R.layout.item_img, parent,false);
        mImageView = (ImageView) convertView.findViewById(R.id.img);
        del = (ImageView) convertView.findViewById(R.id.del);
        if (position < list.size()) {
            Tools.loadImg(list.get(position), mImageView);
            del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    context.deleImg(position);
                }
            });
        } else {
            del.setVisibility(View.GONE);
            mImageView.setImageResource(R.mipmap.guzhang);//最后一个显示加号图片
            mImageView.setAdjustViewBounds(true);
        }

        return convertView;
    }
}